import Vue from 'vue';
import VueRouter from 'vue-router';
import login from '@/views/login/login.vue';
import layout from '@/views/home/layout.vue';
import chart from '@/views/home/chart/chart.vue';
import userList from '@/views/home/userList/userList.vue';
import question from '@/views/home/question/question.vue';
import business from '@/views/home/business/business.vue';
import subject from '@/views/home/subject/subject.vue';

// 解决路由重复点击的问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter);
const router = new VueRouter({
    routes: [{
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            component: login,
        },
        {
            path: '/layout',
            component: layout,
            redirect: '/layout/chart',
            children: [{
                    path: '/layout/chart',
                    meta:{
                        title:'数据概览',
                    },
                    component: chart
                }, {
                    path: '/layout/userList',
                    meta:{
                        title:'用户列表',
                    },
                    component: userList
                }, {
                    path: '/layout/question',
                    meta:{
                        title:'题库列表',
                    },
                    component: question
                }, {
                    path: '/layout/business',
                    meta:{
                        title:'企业列表',
                    },
                    component: business,
                },
                {
                    path: '/layout/subject',
                    meta:{
                        title:'学科列表',
                    },
                    component: subject,
                }
            ]
        },
    ],
});

// 进度条插件
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
// 路由导航守卫：

// 1.进入目标路由前导航
router.beforeEach((to,from,next) => {
    NProgress.start();
    next()
})

// 2.进入目标路由后导航
router.afterEach((to) => {
    document.title = to.meta.title
    NProgress.done();
})
export default router;